package com.wjk.cms.controller;


import com.alibaba.fastjson.JSONObject;
import com.wjk.cms.common.Result;
import com.wjk.cms.pojo.Card;
import com.wjk.cms.pojo.Order;
import com.wjk.cms.pojo.User;
import com.wjk.cms.service.CardService;
import com.wjk.cms.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/card")
public class CardController extends AuthenticatedController {

    @Autowired
    private CardService cardService;

    @Autowired
    private OrderService orderService;







    @PostMapping("/list")
    public Result list(
            @RequestParam(value = "keywords", required = false) String keywords,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "flag", required = false) String flag, // 激活时间 0近一周 1 近一月 2 近三月 3近半年
            @RequestParam(value = "startTime", required = false) String startTime, //激活时间
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo, // 页码
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        log.info("card/list \n" +
                "flag:"+flag);
        User currentUser = getCurrentUser();
        if(!currentUser.getType().equals("2")){
            return Result.error("非经销商没有权限操作");
        }
        String[] statusText = {"初始","已领取","已激活","已提取","已发货"};
        List<Card> list =cardService.list(currentUser.getId(),keywords,status,null,flag,startTime,endTime,pageNo,pageSize);
        for (Card card : list) {
            Order order = orderService.getByCardId(card.getId());
            card.setStatusText(statusText[card.getStatus()]);
            if(order!=null){
                card.setOrderCode(order.getCode());

            }
        }
        List<Card> total =cardService.list(currentUser.getId(),null,"",null,flag,null,null,-1,pageSize);
        List<Card> active =cardService.list(currentUser.getId(),null,"2",null,flag,null,null,-1,pageSize);
        List<Card> pick =cardService.list(currentUser.getId(),null,"3",null,flag,null,null,-1,pageSize);
        List<Card> send =cardService.list(currentUser.getId(),null,"4",null,flag,null,null,-1,pageSize);
        JSONObject obj1 = new JSONObject();
        obj1.put("name","已发售");
        obj1.put("num",total.size());
        JSONObject obj2 = new JSONObject();
        obj2.put("name","已激活");
        obj2.put("num",active.size() +pick.size()+send.size());
        JSONObject obj3 = new JSONObject();
        obj3.put("name","已提取");
        obj3.put("num",pick.size()+send.size());
        ArrayList<Object> tab = new ArrayList<>();
        tab.add(obj1);
        tab.add(obj2);
        tab.add(obj3);

        return Result.ok().add("data", list).add("tab",tab);
    }

    @PostMapping("/back")
    public Result active(
            @RequestParam(value = "cardId", required = false) Long cardId
    ) {
        User currentUser = getCurrentUser();
        if(!currentUser.getType().equals("2")){
            return Result.error("非经销商没有权限操作");
        }
        Card card = cardService.getById(cardId);
        if(card.getStatus()==1){
            card.setStatus(0);
            card.setUserId(null);
            cardService.save(card);
        }else{
            return Result.error("非领取状态不能退还");
        }
        return Result.ok();
    }

    @PostMapping("/detail")
    public Result detail(
            @RequestParam(value = "id", required = true) Long id
    ) {
        User currentUser = getCurrentUser();

        Card card = cardService.getById(id);
        if(card.getCustomerId()!= currentUser.getId()){
            return Result.error("非该卡用户没有权限操作");
        }
        return Result.ok().add("card", card);
    }

    @PostMapping("/addAddress")
    public Result addAddress(
            @RequestParam(value = "id", required = true) Long id,
            @RequestParam(value = "address", required = true) String address,
            @RequestParam(value = "name", required = false) String receiverName,
            @RequestParam(value = "phone", required = false) String receiverPhone,
            @RequestParam(value = "remark", required = false) String remark
    ) {
        User currentUser = getCurrentUser();

        Card card = cardService.getById(id);
        if(card.getCustomerId()!= currentUser.getId()){
            return Result.error("非该卡用户没有权限操作");
        }
        card.setStatus(3);
        card.setAddress(address);
        card.setReceiverName(receiverName);
        card.setReceiverPhone(receiverPhone);
        card.setRemark(remark);
        cardService.save(card);
        Order order = orderService.getByCardId(id);
        if(order!=null){
            order.setStatus(2);//待发货
            orderService.save(order);
        }
        return Result.ok().add("card", card);
    }




}
